package com.comunity_cloud.client.model.upload_file;

import java.nio.ByteBuffer;

public class FileChunkHeader {
	
	public static final int SIZE = 28;

	private long version;
	
	private long totalSize;
	
	private long totalOffset;
	
	private int primary;

	public FileChunkHeader() {
	
	}
	
	public FileChunkHeader(long totalSize, long totalOffset, int primary) {
		this.totalSize = totalSize;
		this.totalOffset = totalOffset;
		this.primary = primary;
	}
	
	public long getTotalSize() {
		return totalSize;
	}

	public void setTotalSize(long totalSize) {
		this.totalSize = totalSize;
	}

	public long getTotalOffset() {
		return totalOffset;
	}

	public void setTotalOffset(long totalOffset) {
		this.totalOffset = totalOffset;
	}
	
	public boolean isPrimary(){
		return primary == 1;
	}

	public byte[] asBytes(){
		byte[] bytes = new byte[SIZE];
		ByteBuffer byteBuffer = ByteBuffer.wrap(bytes);
		byteBuffer.putLong(version);
		byteBuffer.putLong(totalSize);
		byteBuffer.putLong(totalOffset);		
		byteBuffer.putInt(primary);
		return bytes;
	}
	
	public static long versionFromBytes(byte[] bytes) {
		ByteBuffer byteBuffer = ByteBuffer.wrap(bytes);
		return byteBuffer.getLong();
	}
	
	public void fromBytes(byte[] bytes){
		ByteBuffer byteBuffer = ByteBuffer.wrap(bytes);
		version = byteBuffer.getLong();
		totalSize = byteBuffer.getLong();
		totalOffset = byteBuffer.getLong();
		primary = byteBuffer.getInt();
	}
	
}
